/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.ui.editors;

import java.io.InputStream;

import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IStorageEditorInput;
import org.zoundry.gaedt.core.models.application.ZGaeApplicationModel;


/**
 * Base class for *.yaml source editor inputs.
 */
public abstract class ZGaeSourceEditorInputBase implements IStorageEditorInput, IStorage
{
    /** Name of the editor input. */
    private String mName;
    /** Full path to the editor input. */
    private IPath mPath;
    /** Editor input stream. */
    private InputStream mInputStream;

    /**
     * C'tor.
     */
    public ZGaeSourceEditorInputBase()
    {
    }

    /**
     * Sets the input to given application model.
     * 
     * @param aModel
     */
    public abstract void setInput(ZGaeApplicationModel aModel);

    /**
     * @see org.eclipse.ui.IStorageEditorInput#getStorage()
     */
    public IStorage getStorage() throws CoreException
    {
        return this;
    }

    /**
     * @see org.eclipse.ui.IEditorInput#exists()
     */
    public boolean exists()
    {
        return mInputStream != null;
    }

    /**
     * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
     */
    public ImageDescriptor getImageDescriptor()
    {
        return ImageDescriptor.getMissingImageDescriptor();
    }

    /**
     * @see org.eclipse.ui.IEditorInput#getName()
     */
    public String getName()
    {
        return mName;
    }

    /**
     * @see org.eclipse.ui.IEditorInput#getPersistable()
     */
    public IPersistableElement getPersistable()
    {
        return null;
    }

    /**
     * @see org.eclipse.ui.IEditorInput#getToolTipText()
     */
    public String getToolTipText()
    {
        return "";
    }

    /**
     * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
     */
    public Object getAdapter(Class aAdapter)
    {
        if (aAdapter.equals(IStorage.class))
        {
            return this;
        }
        return null;
    }

    /**
     * @see org.eclipse.core.resources.IStorage#getContents()
     */
    public InputStream getContents() throws CoreException
    {
        return mInputStream;
    }

    /**
     * @see org.eclipse.core.resources.IStorage#getFullPath()
     */
    public IPath getFullPath()
    {
        return mPath;
    }

    /**
     * @see org.eclipse.core.resources.IStorage#isReadOnly()
     */
    public boolean isReadOnly()
    {
        return true;
    }

    /**
     * @return the path
     */
    protected IPath getPath()
    {
        return mPath;
    }

    /**
     * @param aPath the path to set
     */
    protected void setPath(IPath aPath)
    {
        mPath = aPath;
    }

    /**
     * @return the inputStream
     */
    protected InputStream getInputStream()
    {
        return mInputStream;
    }

    /**
     * @param aInputStream the inputStream to set
     */
    protected void setInputStream(InputStream aInputStream)
    {
        mInputStream = aInputStream;
    }

    /**
     * @param aName the name to set
     */
    protected void setName(String aName)
    {
        mName = aName;
    }
}
